<template>
	<view class="psxidleweb">
		<view class="pisnp" @click='nav'>
			<u--input class='psxinp' placeholder="添加按钮" v-model='value' border='none' suffixIcon="arrow-right"
				suffixIconStyle="color: #909399" @change="change"></u--input>

		</view>
		<view class="upimg">
			<view class="psxinptext">
				<input placeholder="请输入内容" v-model='list.content' />
			</view>

			<!-- <u--textarea  border='none' placeholder="请输入内容" v-model='list.content'></u--textarea> -->

			<!-- <p class='psxaddp'>添加图片</p>	 -->
			<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" @clickPreview="clickPreview" name="1"  :maxCount="10">
			</u-upload>
		</view>
		<u-toast ref="uToast"></u-toast>
		<view class="psxtitle">
			<span>预计价格</span>
			<span>
				<u--input v-model='value1' placeholder="请输入价格" border="none" type='number' style='width: 200upx;'>
				</u--input>
			</span>
		</view>
		<u-button text="提交" border='none' class='psxbtn' @click='postidle'></u-button>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: {
					title: '',
					price: '',
					img: [],
					content: ''
				},
				title: '',
				value1: '',
				value: '',
				fileList1: [],
				userId: 0
			}
		},
		onLoad: function(option) {
			// console.log(option.title)
			this.value = option.title
		},
		created() {
			if (getApp().globalData.userId) {
				this.userId = getApp().globalData.userId
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				console.log(event)
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			clickPreview(event){
				console.log(event)
				// this[`fileList${event.name}`].splice(event.index, 1)
			},
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: `http://127.0.0.1:7001/api/psxloadimg`, // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							resolve(res.data)
							// console.log(res.data.data)
							let data = JSON.parse(res.data)
							this.list.img.push(`http://127.0.0.1:7001${data.data}`)
						}
					});
				})
			},
			change(e) {
				this.value = e

				console.log(this.value, e);
			},
			nav() {
				uni.navigateTo({
					url: '/pages/issue/cation'
				})
				// console.log('1')
			},
			postidle() {
				this.list.title = this.value
				this.list.price = this.value1
				// console.log(this.list.title)
				if (this.list.title == undefined || this.list.price ==''|| this.list.img == []) {
					console.log("11111")
					this.$refs.uToast.show({
						type: 'error',
						message: "请选入数据哦！！",
						iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/error.png'
					})
				} else {
					this.$api.postmjtisell({
						shopName: this.list.title,
						shopImg: this.list.img[0],
						shopTitle: this.list.content,
						shopPrice: this.list.price,
						shopState: '2',
						userId: this.userId,
						collectSize: 0
					}).then(res => {
						// console.log(res)
						if (res.code == 200) {
							this.$refs.uToast.show({
								type: 'success',
								message: "发布成功",
								iconUrl: 'https://cdn.uviewui.com/uview/demo/toast/success.png'
							})
							setTimeout(() => {
								this.value = ''
								uni.switchTab({
									url: '/pages/Home/Home'
								})
							}, 1000)
						}
					})
				}
			}
		}
	}
</script>

<style scoped>
	.psxidleweb {
		width: 100%;
		height: 100vh;
		background-color: ghostwhite;
		margin: 0 auto;
	}

	.pisnp {
		padding: 20upx;
	}

	.psxinp {
		background-color: white;
		height: 100upx;
		/* padding: 10upx; */
	}

	.upimg {
		width: 90%;
		height: 500upx;
		padding: 20upx;
		margin: 20upx auto;
		background-color: white;
	}

	.psxinptext {
		width: 100%;
		height: 120upx;
	}

	.psxbtn {
		margin-top: 300upx;
		border: none;
		width: 90%;
		/* line-height: 20upx; */
		padding-bottom: 20upx;
	}

	.psxaddp {
		color: gainsboro;
		height: 200upx
	}

	.psxtitle {
		background-color: white;
		height: 100upx;
		width: 95%;
		justify-content: space-between;
		align-items: center;
		display: flex;
		margin: auto;
	}
</style>
